package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
  @Bean
  public CorsFilter corsFilter(){
    //创建Cors配置对象
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    //设置哪些域可以访问
    corsConfiguration.addAllowedOrigin("*");
    //设置哪些请求头可以访问
    corsConfiguration.addAllowedHeader("*");
    //设置哪些方法可以访问
    corsConfiguration.addAllowedMethod("*");
    //创建资源对象
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    //添加配置资源
    source.registerCorsConfiguration("/**",corsConfiguration);
    return new CorsFilter(source);
  }
}